﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.CrystalReport;
using MSBTC.WinFormHelpers;
using MSBTC.Model;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace MSBTC.ReportViewer
{
    public partial class LaborRptForm : Form
    {
        FormHelpers help = new FormHelpers();
        PMSContext dbcontext = new PMSContext();

        public LaborRptForm()
        {
            InitializeComponent();
        }

        private void LaborRptForm_Load(object sender, EventArgs e)
        {
            LaborRpt LRpt = new LaborRpt();

            if (help.PrintStat() == "nALL")
            {
                string pname = help.Pname();

                var proj = (from p in dbcontext.Projects
                            where p.Project_Name == pname
                            select p).SingleOrDefault();

                var labr = (from l in dbcontext.Labors
                            where l.Projectid_FK == proj.Projectid_PK
                            select new { l.Labour_Name, l.Net_Paid, l.NumOf_Days, l.OT_Amount, l.Position, l.Rate, l.Total, l.Amount_Week, l.Cash_Advance }).ToList();

                if (labr.Count != 0)
                {
                    TextObject txtobj = (TextObject)LRpt.ReportDefinition.ReportObjects["txtproj"];
                    txtobj.Text = proj.Project_Name;

                    LRpt.SetDataSource(labr);
                    Laborrport.ReportSource = LRpt;

                }
                else
                {
                    MessageBox.Show("Nothing to Print", "Print", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else if (help.PrintStat() == "ALL")
            {
                try
                {
                    TextObject txtobj = (TextObject)LRpt.ReportDefinition.ReportObjects["txtproj"];
                    txtobj.Text = "ALL";

                    Laborrport.ReportSource = LRpt;
                    Laborrport.RefreshReport();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString() + " Nothing to Print.", "Print", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
        }

    }
}
